package org.budo.mybatis.dao.statement.builder.creator;

import java.util.Map;

import org.apache.ibatis.session.Configuration;
import org.budo.mybatis.dao.statement.builder.impl.DefaultStatementBuilder;
import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class SaveIgnoreNullStatementCreator extends AbstractSaveStatementCreator {
    public SaveIgnoreNullStatementCreator(Configuration configuration, DefaultStatementBuilder statementBuilder) {
        super(configuration, statementBuilder);
    }

    protected String getStatementId() {
        return STATEMENT_SAVE_IGNORE_NULL;
    }

    protected String createStatement(Class<?> entityType) {
        String tableName = this.getStatementBuilder().getEntityConfiger().getTableName(entityType);
        String idFieldName = this.getStatementBuilder().getEntityConfiger().getIdFieldName(entityType);

        Map<String, String> columnAndValues = this.insertColumnAndValues(this.getStatementBuilder().getEntityConfiger(), entityType, true, "");

        String statementXml = _4 + "<insert id=\"" + this.getStatementId() + "\" parameterType=\"" + entityType.getName() + "\"" + " useGeneratedKeys=\"true\" keyProperty=\"" + idFieldName + "\">\n"//
                + _8 + "INSERT INTO " + tableName + "\n"//
                + _12 + "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n" //
                + _16 + StringUtil.join(columnAndValues.keySet(), "\n" + _16) + "\n"//
                + _12 + "</trim>\n"//
                + _8 + "VALUES\n"//
                + _12 + "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n" //
                + _16 + StringUtil.join(columnAndValues.values(), "\n" + _16) + "\n"//
                + _12 + "</trim>\n"//
                + _4 + "</insert>"; //

        return statementXml;
    }
}